﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class TMPRULES
    {

        public TMPRULES()
        {
            InitializeComponent();
            MV = new VICC_PRULES();
            this.DataContext = MV;
        }

        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            VSDDAT.SD22.RESETSEQ("0022", "  ", "PRSEQ", 1);
            TMV.DPRULES02(null);
            foreach (DataRow row in TBMain.Rows)
            {
                LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
                row["ROWNID"] = LFD;
                row["PLANID"] = LFD;
                row["ORDERNUM"] = LFD;
                TMV.InsertICC_PRULES(row, app.company);
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
            if (ChkSeq.IsChecked==true)
                return;
            dwData["ROWNID"] = LFD;
            dwData["PLANID"] = LFD;
            dwData["ORDERNUM"] = LFD;            
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_PRULES(dwData);
            base.NewCommand(sender, e);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            SResult = TMV.QPRULES04(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
            dwData["ROWNID"] = LFD;
            dwData["PLANID"] = LFD;
            dwData["ORDERNUM"] = LFD;
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_PRULES(dwData);
            base.CopyCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                TMV.DPRULES01(row);
                row.Delete();
            }
            base.DeleteCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DPRULES02(null);
            dwData = TBMain.Clone().NewRow();
            TMV.SetUpNewRow(ref dwData);
            foreach (DataRow row in TBMain.Rows)
            {
                dwData.ItemArray = row.ItemArray;
                TMV.InsertICC_PRULES(dwData, app.company);
            }
            //TMV.UpdateICC_SRULES(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
         

        private void LoadExcelCommand(object sender, RoutedEventArgs e)
        {
            cDR.PRINT(null);
            if (cDR.SetExchange == null)
                return;
            if (cDR.SetExchange.Tables.Count == 0)
                return;
            foreach (DataRow row in cDR.SetExchange.Tables[0].Rows)
            {
                foreach (DataColumn c in cDR.SetExchange.Tables[0].Columns)
                {
                    row[c] = Helper.ConvertGB2312To8859P1(row[c].ToString());
                }
            }
            MergeData(cDR.SetExchange, true, true);
        }

    }
}
